package top.drinyeit.weilong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.adapter.ConfirmOrderAdapter;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.base.BaseActivity;
import top.drinyeit.weilong.model.AddressDetailBean;
import top.drinyeit.weilong.model.AddressDetailJsonBean;
import top.drinyeit.weilong.model.TemporaryOrderDetailBean;
import top.drinyeit.weilong.model.UserAddressBean;
import top.drinyeit.weilong.presenter.ConfirmOrderPresenter;
import top.drinyeit.weilong.util.SPUtil;

/**
 * Created by wu on 2018/1/11.
 */

public class ConfirmOrderActivity extends BaseActivity{
    public TextView address,consignee_name,consignee_phone,order_price,express_price,commodity_total_price,commodity_total_price0;
    private TemporaryOrderDetailBean temporaryOrder;
    private ImageView back;
    private TextView go_pay;
    public RecyclerView mRecyclerView;
    private ConfirmOrderPresenter confirmOrderPresenter;
    private ConfirmOrderAdapter confirmOrderAdapter;
    private LinearLayout ll_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order);

        initView();
        initListener();
        initData();
    }

    private void initIntent(){
        Bundle bundle = getIntent().getExtras();
        temporaryOrder = (TemporaryOrderDetailBean) bundle.getSerializable("temp_order");
        order_price.setText("¥" + temporaryOrder.getOrder_info().getParent_order_info().getCommodity_total_price());
        express_price.setText("¥" + temporaryOrder.getOrder_info().getParent_order_info().getExpress_price());
        commodity_total_price.setText("¥" + temporaryOrder.getOrder_info().getParent_order_info().getOrder_price());
        commodity_total_price0.setText("¥" + temporaryOrder.getOrder_info().getParent_order_info().getOrder_price());
        setAddress();
    }

    private void initView(){
        go_pay = findViewById(R.id.go_pay);
        address = findViewById(R.id.address);
        consignee_name = findViewById(R.id.consignee_name);
        consignee_phone = findViewById(R.id.consignee_phone);
        order_price = findViewById(R.id.order_price);
        commodity_total_price = findViewById(R.id.commodity_total_price);
        express_price = findViewById(R.id.express_price);
        commodity_total_price0 = findViewById(R.id.commodity_total_price0);
        mRecyclerView = findViewById(R.id.mRecyclerView);
        back = findViewById(R.id.back);
        ll_address = findViewById(R.id.ll_address);
        initIntent();
        initRecyclerView();
        setTitle(true,"确认订单");
    }

    private void initRecyclerView(){
        confirmOrderAdapter = new ConfirmOrderAdapter(temporaryOrder.getOrder_info().getChildren_order_list());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(confirmOrderAdapter);
        mRecyclerView.setNestedScrollingEnabled(false);
    }

    private void initListener(){
        go_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String,Object> map = new HashMap<>();
                if (temporaryOrder.getUser_address() == null){
                    showToast("请选择收货地址");
                    return;
                }
                map.put("action","commodity_add_order");
                map.put("address_id",temporaryOrder.getUser_address().getId());
                map.put("temp_order",new Gson().toJson(temporaryOrder));
                confirmOrderPresenter.addOrder(map);
            }
        });
        ll_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ConfirmOrderActivity.this,MyAddressActivity.class);
                intent.putExtra("clickable",true);
                startActivityForResult(intent,1);
            }
        });
    }

    private void initData(){
        confirmOrderPresenter = new ConfirmOrderPresenter(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1){
            Bundle bundle = data.getExtras();
            if (temporaryOrder.getUser_address() == null){
                AddressDetailJsonBean addressDetailJsonBean = new AddressDetailJsonBean();
                UserAddressBean userAddressBean = new UserAddressBean(addressDetailJsonBean,"","");
                temporaryOrder.setUser_address(userAddressBean);
            }
            temporaryOrder.getUser_address().setDefault_flag(bundle.getInt("default_flag"));
            temporaryOrder.getUser_address().setUser_id(SPUtil.getShareString(Constant.USER_ID));
            temporaryOrder.getUser_address().setId(bundle.getInt("id"));
            temporaryOrder.getUser_address().setAddress_detail((AddressDetailJsonBean)bundle.get("detail"));
            temporaryOrder.getUser_address().setConsignee_name(bundle.getString("consignee_name"));
            temporaryOrder.getUser_address().setConsignee_phone(bundle.getString("consignee_phone"));
            setAddress();
        }
        if (requestCode == 1 && resultCode == 2){
            temporaryOrder.setUser_address(null);
            setAddress();
        }
    }

    private void setAddress(){
        if (temporaryOrder.getUser_address() == null){
            address.setText("请选择收货地址");
            consignee_name.setVisibility(View.GONE);
            consignee_phone.setVisibility(View.GONE);
        }else {
            address.setText(temporaryOrder.getUser_address().getAddress_detail().getDetail());
            consignee_name.setText(temporaryOrder.getUser_address().getConsignee_name());
            consignee_phone.setText(temporaryOrder.getUser_address().getConsignee_phone());
            consignee_name.setVisibility(View.VISIBLE);
            consignee_phone.setVisibility(View.VISIBLE);
        }

    }
}
